package top.infopub.mgr.mkt.web;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.mkt.TenderPlanningDto;
import top.infopub.enums.mkt.ProProcessDetailTypeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.mkt.service.TenderPlanningService;
import top.infopub.model.mkt.BidPlan;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

/**
 * @Author Warking
 * @Date 2021/5/2 13:48
 * @Version 1.0
 */
@Controller
@RequestMapping("/tenderPlanningDto")
public class TenderPlanningController extends GenericFlowController {

    @Autowired
    private TenderPlanningService tenderPlanningService;

    @Autowired
    private ProProcessManageService proProcessManageService;
    @Autowired
    private OAFlowService oaFlowService;

    //标书策划详情页面
    @RequestMapping("/queryDetail")
    @ResponseBody
    public TenderPlanningDto queryDetail(String proCode) {
        TenderPlanningDto dto = tenderPlanningService.queryDetail(proCode);
        return dto;
    }

    //标书策划保存提交
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String tenderPlanningDto, String identifying, String proCode) {
        if (StringUtils.isEmpty(tenderPlanningDto) || StringUtils.isEmpty(proCode) || StringUtils.isEmpty(identifying)) {
            return RspVo.getFailureResponse(0, "数据为空");
        }


        String object = StringEscapeUtils.unescapeJava(tenderPlanningDto);
        TenderPlanningDto dto = JSON.parseObject(object, TenderPlanningDto.class);

        if ("0".equals(identifying)) {//0是保存
          return  saveStatus(dto,proCode);
        } else { //1是提交
            //判断标书策划的状态
            String flag = tenderPlanningService.isFlag(proCode);
            if ("1".equals(flag) || "9".equals(flag))
            {
                return RspVo.getFailureResponse(0, "审批中或审批已完成，修改失败");
            }
            //提交流程  先保存
            int i = tenderPlanningService.save(dto, proCode);
            if (i > 0) {//保存成的情况 走流程
                //根据唯一procode查询业务表SGUID
                BidPlan  bidPlan = tenderPlanningService.selectByProCode(proCode);
                //登录人员信息
                AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
                if (StringUtils.isNotBlank(bidPlan.getOaId())) {
                    oaFlowService.finishOA(bidPlan.getOaId());
                }
                //发起流程
                String process = proProcessManageService.sendProcess(ProProcessDetailTypeEnum.PRO_NODE_TYPE6, object, bidPlan.getId(), proCode, vo);
                if (!StringUtils.isEmpty(process))
                {
                    return RspVo.getSuccessResponse(0, "提交成功");
                }
                return RspVo.getFailureResponse(0, "提交失败");
            } else { //提交-保存失败
                return RspVo.getFailureResponse(0, "提交失败");
            }

        }
    }

    public RspVo saveStatus(TenderPlanningDto dto,String proCode){
        int i = tenderPlanningService.save(dto, proCode);
        if (i > 0) {
            return RspVo.getSuccessResponse(0, "保存成功");
        } else {
            return RspVo.getFailureResponse(0, "保存失败");
        }
    }
}
